package com.hzhk.happyrent.ui.activity.rent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.card.BankCardHotModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.card.BankCardHotAdapter;
import com.hzhk.happyrent.ui.view.listview.MaiTianListview;
import com.hzhk.happyrent.utils.ListUtils;
import com.tamic.novate.Throwable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HotBankActivity extends Activity implements MaiTianListview.OnMaiTianRefreshListener {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.lv_fragment_card)
    MaiTianListview lvFragmentCard;
    private String id;
    private BankCardHotModel bankCardHotModel;
    private BankCardHotAdapter bankCardHotAdapter;
    private List<BankCardHotModel.DataBean.CardsBean> mList = new ArrayList<>();
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_bank);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("bank_id", id);
        map.put("page", "1");
        map.put("limit", "10");

        HttpManager.getInstance().get(API.BANK_CARD_HOT, map, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(HotBankActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {
            }

            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
                lvFragmentCard.stop();
            }

            @Override
            public void onNext(Object tag, String response) {
                bankCardHotModel = new Gson().fromJson(response, BankCardHotModel.class);
                if (page == 1) {
                    mList.clear();
                    mList.addAll(bankCardHotModel.getData().getCards());
                    bankCardHotAdapter.setList(mList);
                    if (bankCardHotModel.getData().getCards().size() < Constants.PAGE_SIZE) {
                        lvFragmentCard.allLoadFinish();
                    }
                } else {
                    if (!ListUtils.isEmpty(bankCardHotModel.getData().getCards())) {
                        mList.addAll(bankCardHotModel.getData().getCards());
                        bankCardHotAdapter.setList(mList);
                        if (bankCardHotModel.getData().getCards().size() < Constants.PAGE_SIZE) {
                            lvFragmentCard.allLoadFinish();
                        }
                    } else {
                        lvFragmentCard.allLoadFinish();
                    }
                }
            }
        });
    }

    private void initView() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        String title = intent.getStringExtra("title");
        activityTitlebarLeft.setImageResource(R.drawable.back);
        activityTitlebarCenter.setText(title);
        activityTitlebarRight.setVisibility(View.GONE);
        lvFragmentCard.setOnMaiTianRefreshListener(this);
        bankCardHotAdapter = new BankCardHotAdapter(this);
        lvFragmentCard.setAdapter(bankCardHotAdapter);
        lvFragmentCard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HotBankActivity.this, LoadWebViewActivity.class);
                intent.putExtra("uil", mList.get(i).getLink());
                intent.putExtra("name", mList.get(i).getBank_title());
                startActivity(intent);
            }
        });

    }

    @Override
    public void onRefresh() {
        page = 1;
        lvFragmentCard.setFooterEnable(true);
        initData();
    }

    @Override
    public void onLoadMore() {
        page++;
        initData();
    }

    @OnClick(R.id.activity_titlebar_left)
    public void onViewClicked() {
        finish();
    }
}
